package com.seedwill.common.dozer;

import java.lang.reflect.Method;

import org.dozer.CustomConverter;

/**
 * DOZER转换器
 * 
 * 泛枚举数据转换
 * 
 * StringEnumAbstractBase
 * JavaStringEnumerationHolderEx
 * 
 * @author kdyi
 *
 */
public class XmlbeansEnumConverter extends ABaseConverterSupport implements CustomConverter  {

	/* (non-Javadoc)
	 * @see org.dozer.CustomConverter#convert(java.lang.Object, java.lang.Object, java.lang.Class, java.lang.Class)
	 */
	public Object convert(Object existingDestinationFieldValue,
			Object sourceFieldValue, Class<?> destinationClass,
			Class<?> sourceClass) {
		if(sourceFieldValue != null){
			  Object result = null;
			  	try {
			  		log.debug("sourceFieldValue instanceof org.apache.xmlbeans.StringEnumAbstractBase: "+(sourceFieldValue instanceof org.apache.xmlbeans.StringEnumAbstractBase));
			  		log.debug("sourceClass!=null && sourceClass.isAssignableFrom(org.apache.xmlbeans.StringEnumAbstractBase.class): "+(sourceClass!=null && sourceClass.isAssignableFrom(org.apache.xmlbeans.StringEnumAbstractBase.class)));
			  		
					//StringEnumAbstractBase
					if((sourceFieldValue instanceof org.apache.xmlbeans.StringEnumAbstractBase)
						|| (sourceClass!=null && sourceClass.isAssignableFrom(org.apache.xmlbeans.StringEnumAbstractBase.class))	
						){
						log.debug("start convert by StringEnumAbstractBase");
						//org.apache.xmlbeans.StringEnumAbstractBase srcFld = (org.apache.xmlbeans.StringEnumAbstractBase)sourceFieldValue;
						if(destinationClass.isAssignableFrom(String.class)){
							result = sourceFieldValue.toString();
						}else{
							if(destinationClass.isPrimitive() && destinationClass.isAssignableFrom(int.class)){
								result = (Integer)existingDestinationFieldValue;
							}else{
								result = Integer.parseInt(existingDestinationFieldValue.toString());
							}
						}
					} else if((sourceFieldValue instanceof org.apache.xmlbeans.impl.values.JavaStringEnumerationHolderEx)
							|| (sourceClass!=null && sourceClass.isAssignableFrom(org.apache.xmlbeans.impl.values.JavaStringEnumerationHolderEx.class))		
						) {
						log.debug("start convert by JavaStringEnumerationHolderEx");
						//JavaStringEnumerationHolderEx
						org.apache.xmlbeans.impl.values.JavaStringEnumerationHolderEx srcFld = (org.apache.xmlbeans.impl.values.JavaStringEnumerationHolderEx)sourceFieldValue;
						if(destinationClass.isAssignableFrom(String.class)){
							result = srcFld.getEnumValue().toString();
						}else{
							if(destinationClass.isPrimitive() && destinationClass.isAssignableFrom(int.class)){
								result = (Integer)existingDestinationFieldValue;
							}else{
								result = Integer.parseInt(existingDestinationFieldValue.toString());
							}
						}
					}else{
						log.debug("start convert by unkown");
						String clazz2 = destinationClass.getName();
						try {
							Class xmlObjClz = Class.forName(clazz2); 
							//调用静态方法
							if(sourceFieldValue instanceof String){ 
								Method mtd = xmlObjClz.getMethod("forString", String.class);
								result = mtd.invoke(xmlObjClz, sourceFieldValue);
							}else{
								if(sourceClass.isPrimitive() && sourceClass.isAssignableFrom(int.class)){
									//result = (Integer)existingDestinationFieldValue;
									Method mtd = xmlObjClz.getMethod("forInt", int.class);
									result = mtd.invoke(xmlObjClz, sourceFieldValue);
								}else{
									//result = Integer.parseInt(existingDestinationFieldValue.toString());
									Method mtd = xmlObjClz.getMethod("forInt", int.class);
									result = mtd.invoke(xmlObjClz, Integer.parseInt(sourceFieldValue.toString()));
								}
							}
						} catch (Exception e) {
							throw new RuntimeException(e);
						} 
					}
				} catch (RuntimeException e) {
					log.warn(
							"Data convert exception[existingDestinationFieldValue:"
									+ existingDestinationFieldValue+(existingDestinationFieldValue !=null ? "("+existingDestinationFieldValue.getClass()+")" : "") + "]"
									+ "[sourceFieldValue:" + sourceFieldValue +(sourceFieldValue !=null ? "("+sourceFieldValue.getClass()+")" : "")+ "]"
									+ "[destinationClass:" + destinationClass + "]"
									+ "[sourceClass:" + sourceClass + "]", e);
					throw e;
				}
			  return result;
		}
		return null;
	}

//	public static void main(String[] args) {
////		//[existingDestinationFieldValue:1.0(class java.lang.String)][sourceFieldValue:1.0(class cn.gov.zjport.houston.schema.manifest.HeadVersionText$Enum)][destinationClass:class java.lang.String][sourceClass:class cn.gov.zjport.houston.schema.manifest.HeadVersionText$Enum]
////		cn.gov.zjport.houston.schema.manifest.HeadVersionText.Enum aa = cn.gov.zjport.houston.schema.manifest.HeadVersionText.Enum.forString("1.0");   
////		System.out.println(aa instanceof org.apache.xmlbeans.StringEnumAbstractBase);
//		
//		int i = 0;
//		if(i == 0) {
//			System.out.println(0);
//		}else if(i == 1) {
//			System.out.println(1);
//		}else {
//			System.out.println("unkown");
//		}
//		
//	}
}
